<?php

namespace common\models\datas;

use webback\models\data\Website;
use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\behaviors\UpdateCacheBehavior;

/**
 * This is the model class for table "{{%wechat}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $token
 * @property string $account
 * @property string $type
 * @property string $key
 * @property string $secret
 * @property string $encoding_aes_key
 * @property string $avatar
 * @property string $qrcode
 * @property string $address
 * @property string $description
 * @property integer $status
 * @property string $created_at
 * @property string $update_at
 * @property integer $web_site_id
 */
class Wechat extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%wechat}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'token', 'account', 'type', 'key', 'secret', 'encoding_aes_key', 'status'], 'required'],
            [['description'], 'string'],
            [['status'], 'integer'],
            [['name'], 'string', 'max' => 16],
            [['token', 'secret', 'encoding_aes_key', 'avatar', 'qrcode', 'address'], 'string', 'max' => 200],
            [['account', 'type', 'key'], 'string', 'max' => 64],
            [['web_site_id'], 'string'],
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['type'] = function () {
            switch ($this->type) {
                case 1:
                    return \Yii::t('common', '订阅号');
                case 2:
                    return \Yii::t('common', '服务号');
                case 3:
                    return \Yii::t('common', '企业号');
            }
        };
        $fields['status'] = function () {
            switch ($this->type) {
                case 0:
                    return \Yii::t('common', '未启用');
                case 1:
                    return \Yii::t('common', '已启用');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['web_site_id'] = function () {
            return $this->website->name;
        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '公众号ID'),
            'name' => Yii::t('app', '公众号名称'),
            'token' => Yii::t('app', '微信服务Token'),
            'account' => Yii::t('app', '微信号'),
            'type' => Yii::t('app', '公众号类型'),
            'key' => Yii::t('app', 'AppID'),
            'secret' => Yii::t('app', 'AppSecret'),
            'encoding_aes_key' => Yii::t('app', '消息加密秘钥'),
            'avatar' => Yii::t('app', '头像地址'),
            'qrcode' => Yii::t('app', '二维码地址'),
            'address' => Yii::t('app', '所在地址'),
            'description' => Yii::t('app', '公众号简介'),
            'status' => Yii::t('app', '状态'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '修改时间'),
            'web_site_id' => Yii::t('app', '分店ID'),
        ];
    }

    public function getWebsite()
    {
        return $this->hasOne(Website::className(), ['id' => 'web_site_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'wechat',
            ],
        ]);

    }
}
